package com.xsherl.leetcode.solution;

import cn.hutool.core.lang.Assert;
import com.xsherl.leetcode.base.ListNode;

public class MergeTwoSortedLists {

    /**
     * 略
     */
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode root = new ListNode(), tail = root;
        while (l1 != null && l2 != null){
            if (l1.val < l2.val){
                tail.next = l1;
                l1 = l1.next;
            } else {
                tail.next = l2;
                l2 = l2.next;
            }
            tail = tail.next;
        }
        if (l1 == null){
            tail.next = l2;
        } else {
            tail.next = l1;
        }
        return root.next;
    }

    public static void main(String[] args) {
        ListNode l1 = ListNode.build(1, 2, 4);
        ListNode l2 = ListNode.build(1, 3, 4);
        ListNode res = new MergeTwoSortedLists().mergeTwoLists(l1, l2);
        System.out.println(res);
        ListNode output = ListNode.build(1, 1, 2, 3, 4, 4);
        Assert.isTrue(res.equals(output));
    }
}
